﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Police_Question_QuestionHistory : System.Web.UI.Page
{
    private bool Flag = true;
    private int PageCount = 15;
    private string ConstDeparmentID;
    protected void Page_Load(object sender, EventArgs e)
    {
        ConstDeparmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!IsPostBack)
        {
            dptDepartmentID.DataSource = Yipusoft.Account.BLL.Department.GetList("Right(DepartmentID,3)='000'");
            dptDepartmentID.DataTextField = "AliasName";
            dptDepartmentID.DataValueField = "DepartmentID";
            dptDepartmentID.DataBind();
            DateTime St = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime Et = new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 1);
            txtStartTime.Text = St.ToString("yyyy-MM-dd");
            txtEndTime.Text = Et.ToString("yyyy-MM-dd");
            Bind(PageCount, 0);
            Flag = true;

        }
    }

    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        Bind(pageSize, PageIndex);
    }


    private void Bind(int pageSize, int pageIndex)
    {

        Question2DataContext db = new Question2DataContext();
        var exam = from o in db.Examination2 select o;
        List<Examination2> list = new List<Examination2>();
        list = exam.ToList();

       
        DateTime Et = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        DateTime St = Et.AddMonths(-1);
        if (txtStartTime.Text.Trim() != "")
        {
            St = DateTime.Parse(txtStartTime.Text);
        }
        if (txtEndTime.Text.Trim() != "")
        {
            Et = DateTime.Parse(txtEndTime.Text);
        }

        var result = (from s in db.SecuritySubject2 where s.Caption.Contains(txtTitle.Text) && (from o in db.Examination2 where o.StartTime >= St && o.EndTime <= Et select o.SID).Contains(s.Id) orderby s.CreateTime descending select s);
        if (ConstDeparmentID.Substring(2, 3).Equals("000"))
        {
            result = (from rs in result where rs.CreateDepartment.Substring(0, 4) == dptDepartmentID.SelectedValue.Substring(0, 4) && rs.CreateDepartment.Substring(5, 1) == ConstDeparmentID.Substring(5, 1) select rs) as IOrderedQueryable<SecuritySubject2>;

        }
        else
        {
            dptDepartmentID.SelectedValue = ConstDeparmentID.Substring(0, 4) + "000";
            dptDepartmentID.Enabled = false;
            if (ConstDeparmentID.Substring(4, 1).Equals("0"))
            {
                result = (from os in result where ((from o in db.Associate2 where o.DepartmentID.Substring(0, 4) == ConstDeparmentID.Substring(0, 4) && o.DepartmentID.Substring(5, 1) == ConstDeparmentID.Substring(5, 1) select o.SID).Contains(os.Id))|| (os.CreateDepartment.Substring(0,4)==ConstDeparmentID.Substring(0,4) && os.CreateDepartment.Substring(5,1)==ConstDeparmentID.Substring(5,1)) select os) as IOrderedQueryable<SecuritySubject2>;
                //result = (from rs in result where (rs.CreateDepartment.Substring(0, 4) == dptDepartmentID.SelectedValue.Substring(0, 4) && rs.CreateDepartment.Substring(5, 1).Equals(ConstDeparmentID.Substring(5, 1))) || (from o in db.Examination2 where o.DepartmentID.Substring(0, 4) == dptDepartmentID.SelectedValue.Substring(0, 4) && o.DepartmentID.Substring(5, 1).Equals(ConstDeparmentID.Substring(5, 1)) select o.SID).Contains(rs.Id) select rs) as IOrderedQueryable<SecuritySubject2>;
            }
            else
            {
                result = (from s in db.SecuritySubject2 where s.Caption.Contains(txtTitle.Text) && (from o in db.Examination2 where o.StartTime >= St && o.EndTime <= Et && o.DepartmentID == ConstDeparmentID select o.SID).Contains(s.Id) orderby s.CreateTime descending select s);
            }
        }

        var obj = result.Skip(pageSize * pageIndex).Take(pageSize);
        this.UserGrid.DataSource = obj;
        ListPageContral1.ItemCount = result.Count();
        this.UserGrid.DataBind();
        //}
    }

    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ListPageContral1.CurrentPageIndex) * ListPageContral1.PageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }

    public string GetHref(object obj)
    {
        if (ConstDeparmentID.Substring(2, 3).Equals("000") || ConstDeparmentID.Substring(4, 1).Equals("0"))
        {
            return "<a href='Analyse2.aspx?ID=" + obj.ToString() + "'>综合统计</a>";
        }
        else
        {
            return "";
        }
    }


    public string GetDepartmentName(object obj)
    {
        try
        {
            if (obj == null)
                return "";
            else
                return Yipusoft.Account.BLL.Department.GetDepartment(obj.ToString()).ShortName;
        }
        catch
        {
            return "";
        };

    }
    protected void lBtnQuery_Click(object sender, EventArgs e)
    {
        if (txtTitle.Text.Trim() == "")
        {
            Flag = true;
            //  Response.Redirect("QuestionList2.aspx");
        }
        else
        {
            Flag = false;
        }
        Bind(PageCount, 0);
    }
    protected void rBtnSoldier_CheckedChanged(object sender, EventArgs e)
    {
        Response.Redirect("~/Police/Question/QuestionHistory.aspx");
    }
}
